package com.hw.one.collect.xinxiang.data;

import com.hw.one.collect.dao.FiveMinDataDao;
import com.hw.one.collect.param.SmallAirParam;
import com.hw.one.collect.xinxiang.dao.SncodeDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/05/17 09:34.
 */
@Service
public class XinxiangPushService {

    @Resource
    private SncodeDao sncodeDao;

    @Resource
    private FiveMinDataDao fiveMinDataDao;

    public List<byte[]> findFiveMinDataByAreaId(String areaId) {
        List<byte[]> vs = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#0.0");
        DecimalFormat df1 = new DecimalFormat("#0.00");
        List<String> snCodeList = sncodeDao.findSncodesByAreaId(areaId);
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("snCodeList", snCodeList);
        SmallAirParam smallAirParam = new SmallAirParam();
        smallAirParam.setSNCodeList(snCodeList);
        List<Map> resultMap = fiveMinDataDao.findRaiseDustFiveMinDataBySNCodeList(smallAirParam);

        for (Map fiveData : resultMap) {
            Map<String, Object> monitorData = (Map<String, Object>) fiveData.get("MONITORDATA");
            double pm25 = null == monitorData.get("CTPM25") ? 0.0 : Double.valueOf(monitorData.get("CTPM25").toString());
            double pm10 = null == monitorData.get("CTPM10") ? 0.0 : Double.valueOf(monitorData.get("CTPM10").toString());
            double wd = null == monitorData.get("WD") ? 0.0 : Double.valueOf(monitorData.get("WD").toString());
            double ws = null == monitorData.get("WS") ? 0.0 : Double.valueOf(monitorData.get("WS").toString());
            double hum = null == monitorData.get("HUM") ? 0.0 : Double.valueOf(monitorData.get("HUM").toString());
            double temp = null == monitorData.get("TEMP") ? 0.0 : Double.valueOf(monitorData.get("TEMP").toString());
            String sncode = fiveData.get("SNCODE").toString();
            String[] codes = new String[13];
            codes[0] = "01";
            codes[1] = "HW";
            codes[2] = "V1.0";
            codes[3] = sncode;
            codes[4] = df.format(temp);
            codes[5] = df.format(hum);
            codes[6] = df1.format(pm25);
            codes[7] = df1.format(pm10);
            codes[8] = "0.0";
            codes[9] = "0.00";
            codes[10] = df1.format(ws);

            if (wd >= 0 && wd < 22.5)
                codes[11] = "ES";
            if (wd >= 22.5 && wd < 67.5)
                codes[11] = "E";
            if (wd >= 67.5 && wd < 112.5)
                codes[11] = "EN";
            if (wd >= 112.5 && wd < 157.5)
                codes[11] = "N";
            if (wd >= 157.5 && wd < 202.5)
                codes[11] = "WN";
            if (wd >= 202.5 && wd < 247.5)
                codes[11] = "W";
            if (wd >= 247.5 && wd < 292.5)
                codes[11] = "WS";
            if (wd >= 292.5)
                codes[11] = "S";
            codes[12] = sncode;

            String body = "";
            for (String code : codes) {
                body += (code + "|");
            }

            String l = "";
            body = body.substring(0, body.length() - 1);
            int bodylen = body.length();

            if (bodylen >= 10 && bodylen < 100)
                l = "0" + String.valueOf(bodylen);
            else
                l = String.valueOf(bodylen);

            body = l+body  ;
            byte[] bytebody = body.getBytes();

            byte[] result = new byte[bytebody.length + 4];
            result[0] = 0x02;
            System.arraycopy(bytebody, 0, result, 1, bytebody.length);
            result[result.length - 1] = 0x0A;
            result[result.length - 2] = 0x0D;
            result[result.length - 3] = 0x03;

            vs.add(result);
        }
        return vs;
    }

}
